package com.haiwei.poi.excel.manager;

import com.haiwei.poi.word.manager.HaiweiPatternUtil;
import org.junit.Assert;
import org.junit.Test;

/**
 * HaiweiPatternUtil测试类.
 *
 * @date: 2022-06-25
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class HaiweiPatternUtilTester {
    
    /**
     * 测试哪些是需要跟后边的字符合并
     */
    @Test
    public void isNeededMergeRunTest(){
        Assert.assertTrue( HaiweiPatternUtil.isNeededMergeRun("#"));
        Assert.assertTrue( HaiweiPatternUtil.isNeededMergeRun("##"));
        Assert.assertTrue( HaiweiPatternUtil.isNeededMergeRun("##{"));
        Assert.assertFalse( HaiweiPatternUtil.isNeededMergeRun("##{}"));
        Assert.assertFalse( HaiweiPatternUtil.isNeededMergeRun("##{AAAAA}"));
        Assert.assertFalse( HaiweiPatternUtil.isNeededMergeRun("##{  }"));
        // l
        Assert.assertFalse( HaiweiPatternUtil.isNeededMergeRun("##={begin:items_item}"));
        Assert.assertFalse( HaiweiPatternUtil.isNeededMergeRun("###={begin:items_item}"));
    }
    
    /**
     * 测试占位符
     */
    @Test
    public void containPlaceHolderTest(){
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("#"));
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("##"));
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("##{"));
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("##{}"));
       
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("##{  }"));
    
        Assert.assertTrue( HaiweiPatternUtil.containPlaceHolder("##{AAAAA}"));
        Assert.assertTrue( HaiweiPatternUtil.containPlaceHolder("##{AA.BB}"));
        Assert.assertTrue( HaiweiPatternUtil.containPlaceHolder("##{AA.BB.CC}"));
        
        Assert.assertTrue( HaiweiPatternUtil.containPlaceHolder("##{AA1.BB1}"));
        
        Assert.assertTrue( HaiweiPatternUtil.containPlaceHolder("##{AA1.22}"));
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("##{**.**}"));
        Assert.assertFalse( HaiweiPatternUtil.containPlaceHolder("##{@@.@@}"));
    
        Assert.assertTrue( HaiweiPatternUtil.containPlaceHolder("AAA#######{aa}}}}}BBB"));
    }
    
    /**
     * 解析标签中的内容
     * ##{客户1.名称1:width=35&height=35&unit=px&limit=9&row=3}
     */
    @Test
    public void parsePlaceHolderTest(){
        // 因为 word对不同 类型字符给的 run不同，所以最复杂场景是 #######{aa}}}}}
        Assert.assertEquals("AAA#####",HaiweiPatternUtil.parsePlaceHolder("AAA#######{aa}}}}}BBB")[0]);
        Assert.assertEquals("aa",HaiweiPatternUtil.parsePlaceHolder("AAA#######{aa}}}}}BBB")[1]);
        Assert.assertEquals("}}}}BBB",HaiweiPatternUtil.parsePlaceHolder("AAA#######{aa}}}}}BBB")[2]);
    
        // 这里场景其实不会出现，因为：@$&* 1 我 [空格] AAA ##### 是不通run对象
        Assert.assertEquals("@$&*1我 AAA#####",HaiweiPatternUtil.parsePlaceHolder("@$&*1我 AAA#######{aa}}}}}BBB@$&*1我 ")[0]);
        Assert.assertEquals("aa",HaiweiPatternUtil.parsePlaceHolder("@$&*1我 AAA#######{aa}}}}}BBB@$&*1我 ")[1]);
        Assert.assertEquals("}}}}BBB@$&*1我 ",HaiweiPatternUtil.parsePlaceHolder("@$&*1我 AAA#######{aa}}}}}BBB@$&*1我 ")[2]);
        
        Assert.assertEquals("abc",HaiweiPatternUtil.parsePlaceHolder("##{abc}")[1]);
        Assert.assertEquals("123",HaiweiPatternUtil.parsePlaceHolder("##{123}")[1]);
        Assert.assertEquals("abc.123",HaiweiPatternUtil.parsePlaceHolder("##{abc.123}")[1]);
        Assert.assertEquals("&&.abc",HaiweiPatternUtil.parsePlaceHolder("##{&&.abc}")[1]);
        Assert.assertEquals("客户1.名称1:width=35&height=35&unit=px&limit=9&row=3"
                ,HaiweiPatternUtil.parsePlaceHolder("##{客户1.名称1:width=35&height=35&unit=px&limit=9&row=3}")[1]);
    }
}
